package com.qf.controller;

import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {
    @Autowired
    DtsGoodsService dtsGoodsService;
    @Autowired
    DtsUserService dtsUserService;
    @Autowired
    DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    DtsOrderService dtsOrderService;
    @Autowired
    DtsCategoryService dtsCategoryService;


    @RequestMapping("/chart")
    public Object chart(){
        Map<String,Object> data = new HashMap<>();

        List<CategorySellAmts> category= dtsCategoryService.findCategoryShellData();

        String [] categoryNames = new String[category.size()];
        for (int i = 0; i <category.size(); i++) {
            CategorySellAmts categorySellAmts = category.get(i);
            String name = categorySellAmts.getName();

            categoryNames[i]=name;
        }
        CategorySellVo categorySellVo = new CategorySellVo();
        categorySellVo.setCategoryNames(categoryNames);
        categorySellVo.setCategorySellData(category);

        //查询最近一个月的订单总额和订单数量
        List<DayStatis> orderStatis=dtsOrderService.findOrderAtms();

        List<String> dayDataList = fetchDateData(orderStatis);

        String[] dayData = dayDataList.toArray(new String[]{});
        OrderAmtsVo orderAmtsVo=new OrderAmtsVo();

        orderAmtsVo.setDayData(dayData);

        List<BigDecimal> orderAmts = orderStatis.stream().map(orderStati -> {
            return orderStati.getAmts();
        }).collect(Collectors.toList());
        orderAmtsVo.setOrderAmtData(orderAmts.toArray(new BigDecimal[]{}));

        List<Integer> cnsList = orderStatis.stream().map(orderStati -> {
            return orderStati.getCnts();
        }).collect(Collectors.toList());
        Integer[] orderCnts = cnsList.toArray(new Integer[]{});
        orderAmtsVo.setOrderCntData(orderCnts);


        //统计一个月内 用户和订单 每天的数量

        List<DayStatis> userStatis=dtsUserService.findUserStatis();

        UserOrderCntVo userOrderCntVo=new UserOrderCntVo();
        userOrderCntVo.setDayData(dayData);
        userOrderCntVo.setOrderCnt(orderCnts);

        List<Integer> userCnts = userStatis.stream().map(DayStati -> {
            return DayStati.getCnts();
        }).collect(Collectors.toList());
        userOrderCntVo.setUserCnt(userCnts.toArray(new Integer[]{}));

        data.put("categorySell",categorySellVo);
        data.put("orderAmts",orderAmtsVo);
        data.put("userOrderCnt",userOrderCntVo);
        return ResponseUtil.ok(data);
    }

    private List<String> fetchDateData(List<DayStatis> orderStatis) {
        return  orderStatis.stream().map(orderStati->{
            return orderStati.getDayStr();
        }).collect(Collectors.toList());

    }


    @RequestMapping("")
    public Object dashboard(){
        Map<String,Object> data = new HashMap<>();
        Integer goodsTotal=dtsGoodsService.findCount();
        data.put("goodsTotal",goodsTotal);
        Integer userTotal=dtsUserService.findCount();
        data.put("userTotal",userTotal);
        Integer productTotal=dtsGoodsProductService.findCount();
        data.put("productTotal",productTotal);
        Integer orderTotal=dtsOrderService.findCount();
        data.put("orderTotal",orderTotal);

        return ResponseUtil.ok(data);
    }
}
